//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "framework\fn_updateMarker.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_is_server_only","_mission_instance","_marker_params","_marker_name","_delete_marker","_i","_marker_pos","_current_marker","_mission_markers","_title","_jip_instance","_jip_markers"];
DEFAULT_PARAM(_marker_params,0,[]);
DEFAULT_PARAM(_mission_instance,1,TMF_SERVER_INSTANCE_ID);	
DEFAULT_PARAM(_is_server_only,2,false);	

if (_mission_instance == "") then {_mission_instance = TMF_SERVER_INSTANCE_ID;};

_marker_name = GETVAL(_marker_params,0,"");
_delete_marker= (count _marker_params == 1);
if (_marker_name != "") then {
	if (_is_server_only && !IS_DEBUG) then {
		// Hides the marker in case someone is hosting the game on his computer otherwise he sees it.
		SETVAL(_marker_params,10, 0.0000001);
	};
	If (GVAR(Server)) then {
		// If the marker needs to be synchronized in JIP, we add/update it in the JIP list.
		if (!_is_server_only) then {		
			if (isnil QGVAR(STORING_OBJECT_BUSY)) then { GVAR(STORING_OBJECT_BUSY) = -1; };
			waituntil {(GVAR(STORING_OBJECT_BUSY) < time)};
			GVAR(STORING_OBJECT_BUSY) = time + 10;
			_jip_instance = [_mission_instance] call FFUNC(getMissionInstance);
			_jip_markers = [_jip_instance, TMF_HK_MISSION_MARKERS,[],THIS_FILE] call FFUNC(hashGet);

			_marker_pos = -1;
			If ((count _jip_markers)>0) then {
				for "_i" from 0 to ((count _jip_markers) - 1) step 1 do {
					_current_marker = _jip_markers select _i;
					if (str(_current_marker select 0) == str(_marker_name)) exitWith{_marker_pos = _i};
				};
			};
			if (_delete_marker) then {
				if (_marker_pos >= 0) then {
					_jip_markers set [_marker_pos, ["XxDeLxX"]];
					_jip_markers = _jip_markers - ["XxDeLxX"];
					TRACE_2("JIP Marker Deleted",_marker_name,_marker_pos);
				};
			} else {
				if (_marker_pos < 0) then {
					_jip_markers set [count _jip_markers, [_marker_name,_marker_params]];
					TRACE_2("JIP Marker Added",_marker_name,count _jip_markers);
				} else {
					_jip_markers set [_marker_pos, [_marker_name,_marker_params]];
					TRACE_2("JIP Marker Updated",_marker_name,_marker_pos);
				};
			};
			[_jip_instance, TMF_HK_MISSION_MARKERS,_jip_markers] call FFUNC(HashSet);
			[_mission_instance,_jip_instance] call FFUNC(setMissionInstance);
			TRACE_2("JIP Markers Updated",_marker_name,_marker_pos);
			GVAR(STORING_OBJECT_BUSY) = -1;
		};
	};
	// If the marker is server only, we only add it to the server or the client if not multiplayer.
	if ((!_is_server_only) OR (_is_server_only && GVAR(SERVER))) then {
		if (isnil QGVAR(UPDATE_MARKER_BUSY)) then { GVAR(UPDATE_MARKER_BUSY) = -1; };
		waituntil {(GVAR(UPDATE_MARKER_BUSY) < time)};
		GVAR(UPDATE_MARKER_BUSY) = time + 10;
		_mission_markers = [_mission_instance, TMF_HK_MISSION_MARKERS,[]] call FFUNC(getMissionVar);	
		_marker_created_pos = -1;
		if (count _mission_markers	> 0) then {
			for "_i" from 0 to ((count _mission_markers) - 1) step 1 do {
				_current_marker = _mission_markers select _i;
				if (str(_current_marker) == str(_marker_name)) exitWith{_marker_created_pos = _i};
			};			
		};
		if (_delete_marker) then {
			If (_marker_created_pos > 0) then {
				_mission_markers set [_marker_created_pos,"XfXXDEL"];
				_mission_markers = _mission_markers - ["XfXXDEL"];
				TRACE_1("Deleted Marker Info",_marker_created_pos);
			};			
			deleteMarkerLocal _marker_name;
			TRACE_1("Deleted Local Marker",_marker_name);
		} else {
			If (_marker_created_pos < 0) then {
				_marker_params call FFUNC(createMarkerLocal);
				_mission_markers set [count _mission_markers,_marker_name];
				TRACE_1("Created Local Marker",_marker_name);
			} else {		
				_marker_params call FFUNC(createMarkerLocal);
				_mission_markers set [_marker_created_pos,_marker_name];
				TRACE_1("Updated Local Marker",_marker_name);
			};			
		};
		[_mission_instance, TMF_HK_MISSION_MARKERS,_mission_markers] call FFUNC(SetMissionVar);
		GVAR(UPDATE_MARKER_BUSY) = -1;
	};
} else {
	ERROR("Marker has no name");
};
TRACE("End");
TMFPROFILERSTOP;